var app = getApp()
Page({
  data: {
    navH: app.data.navHeight,
  },
  onLoad(option) {
    var left = this;
    this.data.eventChannel = this.getOpenerEventChannel();
    this.data.eventChannel.on('TransferOfData2', function (data) {
      left.data.url = data.url;
      left.data.typeurl = data.typeurl;
      left.data.executeType = data.executeType;
    })
  },
  onShow() {
    var bindTel = app.data.jsonPage.bindTel;
    var nvabarData = {
      title: bindTel.title,
      showCapsule: true
    }
    this.setData({
      nvabarData: nvabarData,
      btnText: bindTel.btnText,
      btnintroduce: bindTel.btnintroduce
    })
    this.getCode()
  },
  getCode() {
    wx.login({
      success: res => {
        const code = res.code;
        this.setData({
          code: code
        })
      },
    })
  },

  getPhoneNumber(e) {
    console.log(e)
    var data = e.detail;
    let that = this
    if (this.data.code) {
      if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
        wx.showToast({
          title: '获取失败',
          icon: 'none'
        })
      } else {
        let data = e.detail
        data.iv = data.iv
        data.code = this.data.code
        this.postTel(data)
      }
    } else {
      wx.login({
        success: res => {
          const code = res.code;
          let data = e.detail
          data.iv = data.iv
          data.code = this.data.code
          that.postTel(e)
        },
      })
    }
  },
  postTel(data) {
    var left = this;
    wx.$.fetch('api/member/login', {
      method: 'post',
      data: data
    }).then(res => {
      wx.setStorageSync("phone", res.data.data.member.mobile);
      wx.setStorageSync('vs-info', res.data.data.member);
      wx.setStorageSync("vs-token", res.data.data.token);
      if (res.data.code == 200) {
        if (left.data.typeurl) {
          wx.redirectTo({
            url: left.data.url
          })
        } else {
          setTimeout(() => {
            wx.navigateBack({
              delta: 2
            })
            if (left.data.executeType) left.data.executeType.emit('monitoringData', { "type": true });
          }, 500)
        }
      }else{
        wx.showToast({
          title: res.data.msg,
          icon:"none",
          duration:1000
        })
      }
    })
  }
})